package com.sayp.netdisk.controller;

import com.sayp.netdisk.pojo.Code;
import com.sayp.netdisk.pojo.Info;
import com.sayp.netdisk.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("file")
public class ShareController {
    @Autowired
    private Info info;

    @Autowired
    private ShareService shareService;
    /**
     * 分享文件的下载链接
     * @param fid 文件id
     * @param time 链接有效时间（ms）
     * @return 文件的下载链接
     */
    @PutMapping("/share/{fid}")
    public Info shareLink(@PathVariable("fid") int fid, long time){
        info.setStatus(true);
        String code = shareService.shareFile(fid, time);
        if(code==null){
            info.setStatus(false);
            return info;
        }
        info.setMsg(code);
        return info;
    }
    /**
     * 取消分享文件
     * @param fid 文件id
     * @return
     */
    @DeleteMapping("/cancel/{fid}")
    public boolean cancelShare(@PathVariable("fid") int fid){
        return shareService.cancelShare(fid);
    }
    /**
     * 根据激活码查询
     * @param code
     * @return
     */
    @GetMapping("/getCode")
    public Code findByCode(String code){
        return shareService.findFileByCode(code);
    }

}
